/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tablasSql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import otros.MiTableModel;
import otros.Util;

/**
 *
 * @author FERRETERIA
 */
public class Articulos {
    
    public static MiTableModel getArticulos(String criterio, String dato, String familia) {
        String[] columNames = {"Codigo","Detalle","Precio Venta","Costo", "Stock"}; 
        String[] tableNames = {"Codigo","Detalle","PrecioVenta","Costo", "stockactual"}; 
        
        String sql = "SELECT Codigo, Detalle, PrecioVenta, Costo,stockactual from narticulos, nstock";
        sql += " where narticulos.id = nstock.articulo";
        String contar = "narticulos";
        String aux = "";
        if (!dato.isEmpty()) {
            sql += " and ";
            contar += " where ";
            if (criterio.compareTo("Detalle") == 0) 
                aux += criterio + " like '%" + dato + "%'";
            else 
                aux += criterio + " = '" + dato + "'";
        }
        sql += aux;
        contar += aux;
        
        int idFamilia = Familias.getIdFamilia(familia);
        if (idFamilia > -1){
            sql += " and idFamilia = " + idFamilia;
            if (dato.isEmpty()) 
                contar += " where idFamilia = " + idFamilia;
            else
                contar += " and idFamilia = " + idFamilia;
        }
        
        
        
        Class[] tipos = new Class [] {java.lang.Integer.class, java.lang.String.class,
                                java.lang.Float.class,java.lang.Float.class, java.lang.Float.class};
        
        Object[][] dtDatos = new Sql().GetTabla(tableNames, contar, sql, tipos);
        if (dtDatos.length > 0) {
            //return new Util.MiDefaultTableModel(dtDatos,columNames); 
            MiTableModel  mt = new MiTableModel(dtDatos,columNames);        
            //mt.setType(tipos);
            return mt;
        } 
        else
            return null;
    }
    
    public static MiTableModel getArticulos(Vector<String> camposS,Vector<String> camposT, String familia, 
                                                 String buscar) {  
        String sql = "SELECT Codigo, Detalle";
        Vector<Class> tipos = new Vector<Class>();
        tipos.add(Integer.class);
        tipos.add(String.class);
        for (int i = 0; i < camposS.size(); i++) {
            sql += "," + camposS.get(i);
            switch (camposS.get(i)) {
                case "stockactual":
                    tipos.add(Float.class);
                    break;
                case "costo":
                    tipos.add(Float.class);
                    break;
                case "precioventa":
                    tipos.add(Float.class);
                    break;
                case "costo*stockactual":
                    tipos.add(Float.class);
                    break;
                case "precioVenta*stockactual":
                    tipos.add(Float.class);
                    break;
            }
        }
        sql +=  " from narticulos, nstock where narticulos.id = nstock.articulo";
        String contar = "narticulos";
        String aux = "";
        int idFamilia = Familias.getIdFamilia(familia);
        
        Vector<String> ops = new Vector<String>();
        if (idFamilia > -1) {
            ops.add(" idfamilia = " + idFamilia);
        }
        if (!buscar.isEmpty())
            ops.add(" detalle like '%" + buscar + "%'");
        
        for(int i = 0; i < ops.size(); i++) {
            sql += " and " + ops.get(i);
            if (i > 0)
               contar += " and " + ops.get(i);
            else
                contar += " where " + ops.get(i);
        }
        
        Object[][] dtDatos = new Sql().GetTabla(camposS.toArray(new String[0]), contar, sql, 
                                    tipos.toArray( new Class[tipos.size()]));
        
        //return new Util.MiDefaultTableModel(dtDatos, camposT.toArray(new String[0])); 
        MiTableModel mt = new MiTableModel(dtDatos, camposT.toArray(new String[0])); 
        tipos.add(Integer.class);
        mt.setType(tipos.toArray( new Class[tipos.size()]));
        return mt;
    }
    
    
    public static ResultSet getArticulo (String cod) {
        String sql = "Select * from narticulos where codigo = '" + cod + "'";
        return new Sql().getDatos(sql);
    }
    
    public static ResultSet getArticulo (String criterio, String dato) {
        String sql = "Select * from narticulos";
        if (criterio.compareTo("Detalle") == 0) 
                sql += " where " + criterio + " like '%" + dato + "%'";
            else 
                sql += " where " + criterio + " = '" + dato + "'";
        return new Sql().getDatos(sql);
    }
    
    
    public static void guardar (String [] datos, boolean guardar) {
        int numString = 6;
        int numFloat = 10;
        String sql = "";   
        if (!guardar) {
           sql = "update narticulos set detalle = ?, codigoasociado = ?, codigobarras1 = ?, codigobarras2 = ?, ";
           sql += "localizacion = ?, codigo = ?, costo = ?, margen = ?, precioventa = ?, igic=?, idfamilia = ? where id = ?";
        }
        else {
            sql = "insert into narticulos (detalle, codigoasociado, codigobarras1, codigobarras2, localizacion, codigo, ";
            sql += "costo, margen, precioventa, igic, idfamilia, id) values (?,?,?,?,?,?,?,?,?,?,?,?)";
        }
        new Sql().actualizarDatos(sql, datos, numString, numFloat);
    }
    
    public static void borrar(String id) {
        String sql = "delete from narticulos where id = " + id;
        new Sql().hacerConsulta(sql);
    }
    
    public static String asignarId(String cod) {
        String sql = "insert into narticulos (codigoBarras1, idFamilia) values (?, 1)";
        String datos [] = {cod};
        Sql cSql = new Sql();
        int id = cSql.actualizarDatos(sql, datos, 1, 0); 
        sql = "update narticulos set codigo = ? where id = ?";
        String datos2 [] = { ""+id, ""+id };
        cSql.actualizarDatos(sql, datos2, 0, 0);
        return ""+id;
    }
    
    public static void actualizarArtCompra(String[] datos) {
        String sql = "update narticulos set detalle = ?, costo = ?, igic = ? where codigo = ?";
        new Sql().actualizarDatos(sql, datos, 1, 3);
    }

    public static boolean is(int cod) {
        String sql = "select * from narticulos where codigo = " + cod;
        return new Sql().is(sql);
    }
    
    public static int getId(String codigo) {
       int id = -1;
       try {
            ResultSet res = new Sql().getDatos("select id from narticulos where codigo = " + codigo);
            if (res.next())
                id = res.getInt("id");
       } catch (SQLException ex) {
       }
       return id;
    }
    
    public static String getMaxCodigo() {
       String codigo = null;
       try {
           String sql = "select codigo from narticulos where id = (select max(id) from narticulos)";
           ResultSet res = new Sql().getDatos(sql);
           if (res.next()) {
                codigo = res.getString("codigo");
           }
       } catch (SQLException ex) {
       }
       return codigo;
    }
}
